package cn.wsgfz.test;

import java.util.List;

import cn.wsgfz.testsvgapp.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class VSvgPngAdapter extends AbsAdapter<Drawable> {
	
	public VSvgPngAdapter(Context c, List<Drawable> resource, int col) {
		super(c, resource, col);
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if(view == null) {
			Holder holder = new Holder();
			LayoutInflater inflater = LayoutInflater.from(getContext());
			view = inflater.inflate(R.layout.diffi_image_adapter, null);
			holder.imageView = (ImageView) view.findViewById(R.id.image_item);
			holder.tvFlagTag = (TextView) view.findViewById(R.id.flag_tab);
			view.setTag(holder);
		}
		
		Holder holder = (Holder) view.getTag();
		Drawable dpng = getResource().get(getResource().size() / 2 - 1);
		setView(holder, getResource().get(position), dpng, position % 2 == 0);
		
		return view;
	}
	
	private void setView(Holder holder, Drawable drawable, Drawable dpng, boolean flag) {
		if(flag) {
			int w = drawable.getIntrinsicWidth();
			int h = drawable.getIntrinsicHeight();
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, h);
			holder.imageView.setLayoutParams(lp);
			holder.imageView.setImageDrawable(dpng);
			holder.tvFlagTag.setText("png");
		} else {
			holder.imageView.setImageDrawable(drawable);
			holder.tvFlagTag.setText("svg");
		}
	}

	class Holder {
		private ImageView imageView;
		private TextView tvFlagTag;
	}
}
